package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.DingtalkAttendanceScheduleDao;
import cn.topevery.garden.entity.po.DingtalkAttendanceSchedule;
import cn.topevery.garden.entity.query.DingtalkAttendanceScheduleQuery;
import cn.topevery.garden.entity.vo.DingtalkAttendanceScheduleVO;
import cn.topevery.garden.entity.vo.DingtalkWorkScheduleUserVO;
import cn.topevery.garden.entity.vo.DingtalkWorkScheduleVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbParameter;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2021-09-02
 */
@Repository
public class DingtalkAttendanceScheduleDaoImpl extends BaseDaoImpl<DingtalkAttendanceSchedule> implements DingtalkAttendanceScheduleDao {
    @Override
    public <P extends DbCsp> List<DingtalkAttendanceSchedule> getSearch(P para) {
        String table = "t_dingtalk_attendance_schedule tdas";

        String columns = "tdas.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tdas.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tdas.c_work_day", ScriptConditionEnum.GreaterThanEq, para.beginDate)
                .where(true, "tdas.c_work_day", ScriptConditionEnum.LessThanEq, para.endDate)
        ;
        List<DingtalkAttendanceSchedule> res = new ArrayList<>();
        run(sqlQueryMate.getSql().toString(), CommandType.Text, (dbCmd) -> {

            Iterator var4 = sqlQueryMate.getParameters().iterator();

            while (var4.hasNext()) {
                DbParameter i = (DbParameter) var4.next();
                dbCmd.addParameter(i);
            }
            dbCmd.executeToList(res, DingtalkAttendanceSchedule.class);
        });
        return res;
    }


    @Override
    public DataPager<DingtalkAttendanceScheduleVO> listPage(DingtalkAttendanceScheduleQuery query) {
        String table = "t_dingtalk_attendance_schedule tdas";

        String columns = "tdas.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tdas.c_id desc");
        sqlQueryMate
                .where(true, "tdas.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, DingtalkAttendanceScheduleVO.class);
    }

    @Override
    public void truncate() {
        String table = "t_dingtalk_attendance_schedule";
        String truncateSql = "truncate table ";
        run(truncateSql + table, CommandType.Text, dbCmd -> {
            dbCmd.executeNonQuery();
        });
    }

    @Override
    public DingtalkAttendanceSchedule getEntity(LocalDate workDay, String userId) {
        String table = "t_dingtalk_attendance_schedule tdas";

        String columns = "tdas.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tdas.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tdas.c_work_day", ScriptConditionEnum.Eq, workDay)
                .where(true, "tdas.c_user_id", ScriptConditionEnum.Eq, userId)
        ;
        List<DingtalkAttendanceSchedule> res = new ArrayList<>();
        run(sqlQueryMate.getSql().toString(), CommandType.Text, (dbCmd) -> {

            Iterator var4 = sqlQueryMate.getParameters().iterator();

            while (var4.hasNext()) {
                DbParameter i = (DbParameter) var4.next();
                dbCmd.addParameter(i);
            }
            dbCmd.executeToList(res, DingtalkAttendanceSchedule.class);
        });
        return (res != null && res.size() > 0) ? res.get(0) : null;
    }

    @Override
    public LocalDate getMaxDate() {
        String table = "t_dingtalk_attendance_schedule tdas";

        String columns = "max(c_work_day) date";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tdas.c_db_status", ScriptConditionEnum.Eq, 0)
        ;
        Map<String, Object> result = getOneMap(sqlQueryMate);
        return (result != null && result.get("date") != null) ? ((Date) result.get("date")).toLocalDate() : null;
    }

    @Override
    public List<Map<String, Object>> getSyncUser(LocalDate date) {
        String table = "t_dingtalk_attendance_schedule tdas";
        String columns = "tdas.c_user_id userId";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tdas.c_id desc");
        sqlQueryMate
                .where(true, "tdas.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tdas.c_work_day", ScriptConditionEnum.Eq, date)
        ;
        return getMap(sqlQueryMate);
    }

    @Override
    public DataPager<DingtalkWorkScheduleUserVO> findAllUserPage(int page, int pageSize, String name) {
        String table = "t_dingtalk_user_relation tdur " +
                "JOIN t_sys_user tsu ON tdur.c_pmi_user_id = tsu.c_id " +
                "JOIN t_dingtalk_attendance_schedule tdas ON tdas.c_user_id = tdur.c_user_id";
        String columns = "DISTINCT tdur.c_user_id userId,tsu.c_user_name username,tdas.c_group_id groupId,tdas.c_shift_id shiftId";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        if (!"".equals(name)) {
            sqlQueryMate.where(true, "tsu.c_user_name", ScriptConditionEnum.LikeR, name);
        }
        DataPager<DingtalkWorkScheduleUserVO> dataPager = getDataPager(sqlQueryMate, pageSize, (page - 1), DingtalkWorkScheduleUserVO.class);
        String columns2 = "count(DISTINCT tdur.c_user_id) total";
        SqlQueryMate sqlQueryMate2 = new SqlQueryMate(table).setColumns(columns2);
        if (!"".equals(name)) {
            sqlQueryMate2.where(true, "tsu.c_user_name", ScriptConditionEnum.LikeR, name);
        }
        dataPager.total = ((long) getOneMap(sqlQueryMate2).get("total"));
        return dataPager;
    }

    @Override
    public List<DingtalkWorkScheduleVO> selectScheduleByDate(String date) {
        String table = "t_dingtalk_attendance_schedule tdas " +
                "JOIN t_dingtalk_duty tdd ON tdas.c_shift_id=tdd.c_data_id";
        String columns = "tdas.c_user_id userId,tdd.c_data_id shiftId,tdd.c_name shiftName,tdas.c_work_day workDate";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "date_format(tdas.c_work_day,'%Y-%m')", ScriptConditionEnum.Eq, date);
        return selectList(sqlQueryMate, DingtalkWorkScheduleVO.class);
    }
}
